package com.suge.send.manager;

import java.util.concurrent.ScheduledThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicLong;

import org.springframework.beans.factory.InitializingBean;
import org.springframework.beans.factory.annotation.Autowired;

import com.suge.message.dao.SmsMessageDAO;
import com.suge.order.dao.SmsOrderDAO;
import com.suge.order.dao.UserBizOrderDAO;
import com.suge.send.processor.SmsOrderProcessor;

public class SmsMessageSendManager implements InitializingBean{

	private SmsOrderDAO smsOrderDAO;
	@Autowired
	private UserBizOrderDAO userBizOrderDAO;
	@Autowired
	private SmsMessageDAO smsMessageDAO;

	private static final int THREAD_SIZE = 2;
	
	public static AtomicLong STORE_REPORT_DELAY_TIME = new AtomicLong(5000);  
	
	/**
	 * ����ʱ����һ��������ʱ������Ե�1�룬
	 * ��Ϊ��ʱӦ�ÿ��ܲ���������ݿ������Ϣ��ݿ��ܻ�û����
	 * �ӳ�1�룬��һ��
	 */
	private long initialDelay = 1000;
	
	/**
	 * notify��Ϣ�̳߳�
	 */
    private ScheduledThreadPoolExecutor notifyDataThreadPool;


	public void setNotifyDataThreadPool(
			ScheduledThreadPoolExecutor notifyDataThreadPool) {
		this.notifyDataThreadPool = notifyDataThreadPool;
	}

	public ScheduledThreadPoolExecutor getThreadPool(){
    	return notifyDataThreadPool;
    }


	private void init() {
		/**
		 * �����̷ֱ߳�����������
		 */
		this.notifyDataThreadPool = new ScheduledThreadPoolExecutor(THREAD_SIZE);
	}	

	public void setInitialDelay(long initialDelay) {
		this.initialDelay = initialDelay;
	}
	
	public void afterPropertiesSet() throws Exception {
		init();		
		for (int i = 0; i < 1; i++) {
			SmsOrderProcessor task = new SmsOrderProcessor();
			task.setSmsOrderDAO(this.smsOrderDAO);
			task.setSmsMessageDAO(this.smsMessageDAO);
			task.setUserBizOrderDAO(this.userBizOrderDAO);
			notifyDataThreadPool.scheduleWithFixedDelay(task, initialDelay,STORE_REPORT_DELAY_TIME.get(), TimeUnit.MILLISECONDS);
		}
	}

	public void setSmsOrderDAO(SmsOrderDAO smsOrderDAO) {
		this.smsOrderDAO = smsOrderDAO;
	}

	public SmsOrderDAO getSmsOrderDAO() {
		return smsOrderDAO;
	}
	public UserBizOrderDAO getUserBizOrderDAO() {
		return userBizOrderDAO;
	}

	public void setUserBizOrderDAO(UserBizOrderDAO userBizOrderDAO) {
		this.userBizOrderDAO = userBizOrderDAO;
	}
}
